/* This is the JSON data processing end for homepage */

import { Homepage_Card, Homepage_Reminder } from './hp_card.js';

const homepage_vns_url = "./assets/json/vns_collection.json";
const homepage_cards_url = "./assets/json/anicard_dataset.json";
const homepage_status = {
    visible_display_queue: new DisplayQueue("visible display queue"),
    hidden_display_queue: new DisplayQueue("hidden display queue"),
    filtering_tag_queue: ["", []],
    filtering_keyword_queue: []
}

class DisplayQueue {
    constructor(name) {
        this._queue  = {};
        this._name = name + "";
        this._head = "";
    }

    _keys () {
        return Object.keys(this._queue);
    }

    _id_list (VNS_tag) {
        if(this._keys().length === 0 || this.keys().indexOf(VNS_tag) < 0) {
            console.error("You can not enumerate an empty queue nor a nonexistent display!");
        }
        return Array.from(this._queue[VNS_tag][1], x => Number(x._get_param("card_id")));
    }

    next_tag (VNS_tag) {
        if(this._keys().length === 0 || this.keys().indexOf(VNS_tag) < 0) {
            console.error("You can not read an empty queue nor a nonexistent display!");
        }
        return this._queue[VNS_tag][2];
    }

    _set_head () {

    }

    append (VNS_tag, cards_list = [], next_tag = "", head = false) {
        head = head || false;
        VNS_tag = VNS_tag + "";
        if(this.keys().indexOf(VNS_tag) > -1) {
            console.error(`VNS_tag: ${VNS_tag} has already existed in the queue.`);
            return -1;
        } 
        let count = cards_list.length;
        this._queue[VNS_tag] = [count, cards_list, next_tag]; // single queue template

        return VNS_tag;
    }

    remove (VNS_tag) {
        VNS_tag = VNS_tag + "";
        if(this.keys().indexOf(VNS_tag) < 0) {
            console.log(`VNS_tag: ${VNS_tag} doesn't exist in the queue.`);
            return false;
        }
        delete this._queue[VNS_tag];
        return VNS_tag;
    }

    pushCard (VNS_tag, card_subject) {
        if((this.keys().indexOf(VNS_tag) < 0) || !(card_subject instanceof Homepage_Card)) {
            console.log("Please put in one Homepage_Card element to one specific VNS class.");
            return -1;
        }
        this._queue[VNS_tag][1].push(card_subject);
        this._queue[VNS_tag][1].sort((a, b) => Number(a._get_param("card_id")) - Number(b._get_param("card_id")));
        this._queue[VNS_tag][0]++;
        return card_subject._get_param("card_id");
    }

    delCard (VNS_tag, card_id ) {
        card_id = Number(card_id) || 0;
        if((this.keys().indexOf(VNS_tag) < 0) || Number(card_id) <= 0) {
            console.error("Please put in one card id number to one specific VNS class.");
            return -1;
        }
        let id_list = this._id_list(VNS_tag);
        let card_No_inArray = id_list.indexOf(card_id);
        if(card_No_inArray < 0) {
            return -1;
        }
        this._queue[VNS_tag][1].splice(card_No_inArray, 1);
        this._queue[VNS_tag][0]--;
        return card_id;
    }
}


function hp_JSON_processing (filtering_tag = ["", []], filtering_keyword = []) {
    
    return {
        visible_display_queue: homepage_status["visible_display_queue"],
        hidden_display_queue: homepage_status["hidden_display_queue"]
    };
}

function _create_display_subjects (display_url) {

    $.getJSON(display_url, json => {
        json.forEach((json_item, i, jsonArr) => {
            let display_reminder_subject = new Homepage_Reminder({
                VNS_tag: json_item["VNS_tag"],
                VNS_desc: json_item["VNS_desc"],
                VNS_num: json_item["VNS_num"]
            });
            
            
        });
    });
}